/*
 * @(#)HttpClientUtil.java 2014-6-10下午12:51:48
 * Copyright 2013 sinovatech, Inc. All rights reserved.
 */
package com.seek.util.wechat;

import java.io.*;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.methods.MultipartPostMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.protocol.Protocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 
 * <ul>
 * <li>
 * <b>修改历史：</b><br/>
 * <p>
 * [2014-6-10下午12:51:48]tuxiaolian<br/>
 * TODO
 * </p>
 * </li>
 * </ul>
 */

@SuppressWarnings("deprecation")
public class HttpClientUtil {

	private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

	/**
	 * 
	 * @author 涂小炼
	 * @creationDate. 2014-6-13 下午4:59:33 
	 * @param reqUrl  请求URL
	 * @param outputStr  post 内容  get方法传null
	 * @param outTime   超时时间  单位：秒
	 * @param charsets  字符集
	 * @return    返回响应结果
	 * @throws Exception
	 */
	public static String doHttpPost(String reqUrl, String outputStr, int outTime,String charsets) throws Exception {
		HttpClient httpClient = new HttpClient();

		httpClient.setConnectionTimeout(new Integer(outTime * 1000));
		httpClient.getParams().setParameter("http.protocol.version",HttpVersion.HTTP_1_1);
		httpClient.getParams().setParameter("http.socket.timeout",new Integer(outTime * 1000));
		httpClient.getParams().setParameter("http.protocol.content-charset",charsets);

		PostMethod postMethod = new PostMethod(reqUrl);
		postMethod.setRequestBody(outputStr);

		try {
			httpClient.executeMethod(postMethod);
			if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
				InputStream resStream = postMethod.getResponseBodyAsStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
				StringBuffer resBuffer = new StringBuffer();
				String resTemp = "";
				while((resTemp = br.readLine()) != null) {
					resBuffer.append(resTemp);
				}
				String response = resBuffer.toString();
				return response;
				//return postMethod.getResponseBodyAsString();
			} else {
				throw new Exception("Unexpected failure: "+ postMethod.getStatusLine().toString());
			}
		} catch (Exception e) {
			logger.error("Http协议post方法发送字符流时候出现异常：", e);
			throw new Exception("Http协议post方法发送字符流时候出现异常：", e);
		} finally {
			postMethod.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
	}

	/**
	 *
	 * @author 涂小炼
	 * @creationDate. 2014-6-13 下午4:59:33
	 * @param reqUrl  请求URL
	 * @param params  post 参数
	 * @param outTime   超时时间  单位：秒
	 * @param charsets  字符集
	 * @return    返回响应结果
	 * @throws Exception
	 */
	public static String doHttpPost(String reqUrl,Map<String,String> params, int outTime, String charsets) throws Exception {
		HttpClient httpClient = new HttpClient();

		httpClient.setConnectionTimeout(new Integer(outTime * 1000));
		httpClient.getParams().setParameter("http.protocol.version",HttpVersion.HTTP_1_1);
		httpClient.getParams().setParameter("http.socket.timeout",new Integer(outTime * 1000));
		httpClient.getParams().setParameter("http.protocol.content-charset",charsets);

		PostMethod postMethod = new PostMethod(reqUrl);
		for(Map.Entry<String,String> entry : params.entrySet()) {
			postMethod.addParameter(entry.getKey(),entry.getValue());
		}

		try {
			httpClient.executeMethod(postMethod);
			if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
				InputStream resStream = postMethod.getResponseBodyAsStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
				StringBuffer resBuffer = new StringBuffer();
				String resTemp = "";
				while((resTemp = br.readLine()) != null) {
					resBuffer.append(resTemp);
				}
				String response = resBuffer.toString();
				return response;
				//return postMethod.getResponseBodyAsString();
			} else {
				throw new Exception("Unexpected failure: "+ postMethod.getStatusLine().toString());
			}
		} catch (Exception e) {
			logger.error("Http协议post方法发送字符流时候出现异常：", e);
			throw new Exception("Http协议post方法发送字符流时候出现异常：", e);
		} finally {
			postMethod.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
	}
	
	/**
	 * 上传图片  
	 * @author 涂小炼
	 * @creationDate. 2014-6-25 下午5:48:26 
	 * @param reqUrl   上传URL
	 * @param file   需要上传的文件
	 * @param outTime
	 * @return
	 * @throws Exception
	 */
	public static String upLoadFile(String reqUrl,File file, int outTime) throws Exception {
		logger.info("reqUrl===" + reqUrl + "file.path====" + file.getAbsolutePath());
		HttpClient httpClient = new HttpClient();

		httpClient.setConnectionTimeout(new Integer(outTime * 1000));
		httpClient.getParams().setParameter("http.protocol.version",HttpVersion.HTTP_1_1);
		httpClient.getParams().setParameter("http.socket.timeout",new Integer(outTime * 1000));
		
		MultipartPostMethod postMethod = new MultipartPostMethod(reqUrl);
		FilePart cbFile = new FilePart("media", file);
		postMethod.addPart(cbFile);
		try {
			httpClient.executeMethod(postMethod);
			if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
				return postMethod.getResponseBodyAsString();
			} else {
				throw new Exception("Unexpected failure: "+ postMethod.getStatusLine().toString());
			}
		} catch (Exception e) {
			logger.error("Http协议post方法发送字符流时候出现异常：", e);
			throw new Exception("Http协议post方法发送字符流时候出现异常：", e);
		} finally {
			postMethod.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
	}

	public static String postByHttps(String url, String body,int outTime,String charsets) {
		logger.info(url);
		String result = "";
		Protocol https = new Protocol("https", new HTTPSSecureProtocolSocketFactory(), 443);
		Protocol.registerProtocol("https", https);
		PostMethod post = new PostMethod(url);
		HttpClient client = new HttpClient();
		client.getParams().setParameter("http.protocol.version",HttpVersion.HTTP_1_1);
		client.getParams().setParameter("http.socket.timeout",new Integer(outTime * 1000));
		client.getParams().setParameter("http.protocol.content-charset",charsets);
		try {
			post.setRequestHeader("Content-Type", "application/json");
			post.setRequestBody(body);
			client.executeMethod(post);
			result = post.getResponseBodyAsString();
			Protocol.unregisterProtocol("https");
			return result;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}

		return "error";
	}
	
}
